import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Keyboard;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;

@ScriptManifest(authors={"LastCoder"}, keywords={" Auto"}, name="AutoFletch", version=1.0D, description="Start, all options are in GUI.")
public class AutoFletch extends Script
  implements MessageListener, PaintListener
{
  private static final AutoFletch.Log[] LOGS = { new AutoFletch.Log("Normal Log", 0, 1511), new AutoFletch.Log("Oak Log", 0, 1521), new AutoFletch.Log("Willow Log", 0, 1519), new AutoFletch.Log("Maple Log", 0, 1517), new AutoFletch.Log("Yew Log", 0, 1515), new AutoFletch.Log("Magic Log", 0, 1513) };
  private static final int KNIFE_ID = 946;
  private static final Color COLOR_1 = new Color(0, 0, 0, 155);
  private static final Color COLOR_2 = new Color(0, 0, 0);
  private static final Color COLOR_3 = new Color(255, 255, 255);
  private static final BasicStroke STROKE = new BasicStroke(1.0F);
  private static final Font FONT_1 = new Font("Arial", 0, 17);
  private static final Font FONT_2 = new Font("Arial", 0, 9);

  private static boolean guiOn = false;
  private static AutoFletch.Log single_log;
  private long activityTime;
  private long startExp;
  private long expGained;
  private long startTime;
  private int expHour;

  public boolean onStart()
  {
    new AutoFletch.Gui().setVisible(true);
    while (guiOn) {
      sleep(20);
    }
    this.startExp = this.skills.getCurrentExp(9);
    this.startTime = System.currentTimeMillis();
    return this.game.isLoggedIn();
  }

  private boolean busy()
  {
    return System.currentTimeMillis() - this.activityTime < 8000L;
  }

  private void antiBan()
  {
    int i = random(1, 5);
    switch (i) {
    case 1:
      if (random(1, 25) != 1)
        return;
      this.mouse.move(random(10, 750), random(10, 495));
      return;
    case 2:
      if (random(1, 6) != 1)
        return;
      int j = this.camera.getAngle() + random(-45, 45);
      if (j < 0) {
        j = random(0, 10);
      }
      if (j > 359) {
        j = random(0, 10);
      }
      char c = '%';
      if (random(0, 100) < 50)
        c = '\'';
      this.keyboard.pressKey(c);
      sleep(random(100, 500));
      this.keyboard.releaseKey(c);
      return;
    case 3:
      if (random(1, 15) != 1)
        return;
      this.mouse.moveSlightly();
      return;
    }
  }

  private AutoFletch.state getState()
  {
    if (this.interfaces.get(905).isValid())
      return AutoFletch.state.INTERFACE;
    if (this.inventory.contains(single_log.id)) {
      if (!busy()) {
        return AutoFletch.state.CUT;
      }
      return AutoFletch.state.SLEEP;
    }

    return AutoFletch.state.BANK;
  }

  public int loop()
  {
    switch (AutoFletch.1.$SwitchMap$AutoFletch$state[getState().ordinal()])
    {
    case 1:
      int i;
      if (!this.bank.isOpen()) {
        this.bank.open();
        for (i = 0; (i < 100) && (!this.bank.isOpen()); i++)
          sleep(10);
        sleep(random(800, 1200));
      } else {
        if (this.inventory.contains(single_log.id)) break;
        if (this.inventory.getCount() > 2) {
          this.bank.depositAllExcept(new int[] { 946 });
          for (i = 0; (i < 100) && (this.inventory.getCount() > 2); i++)
            sleep(20);
        }
        if (this.bank.getItem(single_log.id) == null)
          return -1;
        this.bank.withdraw(single_log.id, 0);
        for (i = 0; (i < 100) && (!this.inventory.contains(single_log.id)); i++)
          sleep(10);
        this.bank.close();
        for (i = 0; (i < 100) && (this.bank.isOpen()); i++)
          sleep(10);
      }
      break;
    case 2:
      if (!this.inventory.isItemSelected()) {
        RSItem localRSItem1 = this.inventory.getItem(new int[] { 946 });
        if (localRSItem1 != null) {
          localRSItem1.doAction("Use");
          for (int k = 0; (k < 100) && (!this.inventory.isItemSelected()); k++)
            sleep(10);
        }
      } else if ((this.inventory.isItemSelected()) && (this.inventory.getSelectedItem().getID() != 946))
      {
        this.inventory.clickSelectedItem();
        for (int j = 0; (j < 100) && (this.inventory.isItemSelected()); j++)
          sleep(10);
      } else {
        RSItem localRSItem2 = this.inventory.getItem(new int[] { single_log.id });
        if (localRSItem2 != null) {
          localRSItem2.doAction("Use");
        }
      }
      break;
    case 3:
      this.interfaces.get(905).getComponent(15).doAction("All");
      this.activityTime = System.currentTimeMillis();
      break;
    case 4:
      sleep(200);
      antiBan();
    }

    return random(600, 1200);
  }

  public void messageReceived(MessageEvent paramMessageEvent)
  {
    String str = paramMessageEvent.getMessage();
    if (str.contains("You"))
      this.activityTime = System.currentTimeMillis();
  }

  public void onRepaint(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    long l = System.currentTimeMillis() - this.startTime;
    String str = Timer.format(l);
    if ((this.skills.getCurrentExp(9) - this.startExp > 0L) && (this.startExp > 0L))
    {
      this.expGained = (this.skills.getCurrentExp(9) - this.startExp);
    }
    if ((this.expGained > 0L) && (l > 0L)) {
      this.expHour = (int)(3600L * this.expGained / l);
    }
    localGraphics2D.setColor(COLOR_1);
    localGraphics2D.fillRect(14, 350, 474, 99);
    localGraphics2D.setColor(COLOR_2);
    localGraphics2D.setStroke(STROKE);
    localGraphics2D.drawRect(14, 350, 474, 99);
    localGraphics2D.setFont(FONT_1);
    localGraphics2D.setColor(COLOR_3);
    localGraphics2D.drawString("AutoFletch", 209, 374);
    localGraphics2D.setFont(FONT_2);
    localGraphics2D.drawString("EXP/Hr: " + this.expHour + "k", 18, 390);
    localGraphics2D.drawString("EXP Gained: " + this.expGained, 18, 400);
    localGraphics2D.drawString("Time Ran: " + str, 182, 390);
    localGraphics2D.drawString("Status: " + getState().toString(), 182, 400);
  }
}